package com.hqyj.controller;
import com.hqyj.pojo.Goods;
import com.hqyj.pojo.User;
import com.hqyj.service.GoodsService1;
import com.hqyj.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.SimpleIdGenerator;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;

import java.util.Map;
import java.util.UUID;

@Controller
@RequestMapping("/shop")
public class GoodsController1 {
    @Autowired
    private GoodsService1 goodsService1;
    @RequestMapping("/goods/insert")
    @ResponseBody
   public Map<String,Object> insert(MultipartFile file, HttpServletRequest request, Goods goods) throws IOException {
        if(file!=null&&file.getOriginalFilename()!="") {
            final String filename = file.getOriginalFilename();
            final String suffer = filename.substring(filename.lastIndexOf("."));
            final UUID uuid = UUID.randomUUID();
            final String path = request.getServletContext().getRealPath("static");
            final File upload = new File(path, "images");
            if (!upload.exists()) {
                upload.mkdirs();
            }
            String img = uuid + suffer;
            final File imgFile = new File(upload.getAbsolutePath(), img);
            file.transferTo(imgFile);
           goods.setGoodsPic(img);
        }
     Map<String, Object> map = new HashMap<>();
        int i =goodsService1.insertGoods(goods);
         if (i>0){
            map.put("message",1);
            return map;
      }
         return null;
    }
    @RequestMapping("/goods/delete")
    @ResponseBody
    public Map<String,Object> delete(long goodsId){
        Map<String, Object> map = new HashMap<>();
        int i = goodsService1.deleteGoods(goodsId);
        System.out.println(i);
        if(i>0){
            map.put("message",1);
            return map;
        }
        return null;
    }

}
